<template>
  <div class="user-register">



    <el-upload
        class="avatar-uploader"
        action="api/upload"
        :show-file-list="false"
        :on-success="handleAvatarSuccess"
        :before-upload="beforeAvatarUpload">

      <el-image v-if="form.item.avatar" :src="this.service.baseURL+form.item.avatar" class="avatar">
        <div slot="error" class="image-slot">
          <i class="el-icon-picture-outline"></i>
        </div>
      </el-image>
      <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      <p>设置头像</p>
    </el-upload>
    <el-form  >
      <el-form-item label="昵称">
        <el-input v-model="form.item.name"></el-input>
      </el-form-item>
      <el-form-item label="邮箱">
        <el-input v-model="form.item.mail"></el-input>
      </el-form-item>
      <el-form-item label="手机号">
        <el-input v-model="form.item.phone"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input type="password" v-model="form.item.password"></el-input>
      </el-form-item>
      <el-form-item label="确认密码">
        <el-input type="password" v-model="form.item.password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="registerUser">注册</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import user from "@/api/user";
import service from "@/api";
import image from "@/api/image";

export default {
  name: "UserList",
  data() {
    return {
      service,
      items: [],
      query: {},
      form: {
        item: {
          "avatar": "",
          "mail": "",
          "name": "",
          "password": "",
          "phone": "",
        }
      }
    }
  },
  created() {
  },
  methods: {
    handleAvatarSuccess(res, file) {
      // this.form.item.avatar = URL.createObjectURL(file.raw);
      console.log(res)
      this.form.item.avatar = res;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isLt2M;
    },

    registerUser() {
      user.registerUser(this.form.item).then(res => {
        console.log(res.data)
        if (res.data === true) {
          this.$message({
            message: res.msg,
            type: "success"
          })
          setTimeout(() => {
            this.$router.push({path: "/login"});
          }, 2500)
        }
      }).catch(res => {
        console.log(res);
        this.$message({
          message: '注册失败',
          type: "error"
        })

      });
    },

  }
}
</script>

<style lang="scss" scoped>

.user-register {
  width: 50%;
  margin: 0 auto;
  //display: flex;
  //justify-content: left;
}

.el-form{
  width: 70%;
  margin: 0 auto;
}

.avatar-uploader{
  //margin-top: 40px;
  //border: 1px solid black;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 72px;
  height: 72px;
  display: block;
  border-radius: 50%;
}

</style>
